// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store/index'

import axios from 'axios' // todo 为什么没有改成wsloan的
import Qs from 'qs';
Vue.config.productionTip = false

import  { ConfirmPlugin,ToastPlugin } from 'vux'
Vue.use(ConfirmPlugin)
Vue.use(ToastPlugin)


let [isTest, origin] = [null,  window.document.location.origin] ;
if(origin.indexOf("192.168") > -1 ||  origin.indexOf("localhost") > -1){
  isTest = true
}else{
  isTest = false
}
const url = isTest ?  'http://192.168.3.26:5821' : 'http://47.97.24.87:5821' 

// const url = 'http://47.97.24.87:5821/' // 正式


let initAjax = (method) => {
  return (_this, {api, data}, baseUrl) => {
    let innerUrl;
    // let headers = localStorage.getItem("token") ? {Authorization: localStorage.getItem("token")} : null;
    if (baseUrl !== undefined) {
      innerUrl = baseUrl;
    } else {
      innerUrl = url;
    }
    return _this.$ajax({
      url: innerUrl + api,
      method,
      // headers,
      data,
      success: () => {
      },
    })
  }
};

window.initAjax = initAjax;
Vue.prototype.$get = initAjax('get');
Vue.prototype.$post = initAjax('post');
Vue.prototype.$http = axios;

window.Vue = Vue;
// 重新构造ajax[ 增添headers ]
let ajax = function (option) {
  let {url, method, headers = null, data, success} = option;
  let config = {
    url,
    method,
  };
  // 判断请求头
  if (headers) {
    Object.assign(config, {headers})
  }
  // 根据请求方法设置请求参数
  console.log('111'+method);
  method.toLowerCase() === 'get' ? Object.assign(config, {params: data}) : Object.assign(config, {data});
  if (method.toLowerCase() === 'post') {
    config.data = Qs.stringify(config.data);
  }
  let promise = axios(config);

  if (success && typeof success === 'function') {
    promise.then(res => success(res))
  }
  return promise
};

Vue.prototype.$ajax = ajax;




/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: {
    App
  }
})
